home *** CD-ROM | disk | FTP | other *** search
- /* XAutoPtr.h
- *
- * Support templates that I can use that are not yet defined
- * for me.
- */
-
- /* YAAF - Yet another application framework
- * Copyright (C) 1997 William Edward Woody and In Phase Consulting
- *
- * This library is free software; you can redistribute it
- * and/or modify it under the terms of the GNU Library
- * General Public License as published by the Free Software
- * Foundation; either version 2 of the License, or any
- * later version.
- *
- * This library is distributed in the hope that it will be
- * useful, but WITHOUT ANY WARRANTY; without even the implied
- * warranty of MERCHANTABIILITY or FITNESS FOR A PARTICULAR
- * PURPOSE. See the GNU Library General Public License for
- * more details.
- *
- * You should have received a copy of the GNU Library General
- * Public License along with this library; if not, write to the
- * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
- * Boston, MA 02111-1307, USA.
- *
- * To contact the author, either e-mail me at
- * woody@alumni.caltech.edu, or write to us at
- *
- * William Edward Woody
- * In Phase Consulting
- * 1545 Ard Eevin Avenue
- * Glendale, CA 91202
- */
-
- #ifndef __XAUTOPTR_H__
- #define __XAUTOPTR_H__
-
- #if defined(__MWERKS__)
- #if defined(macintosh)
- #pragma options align=power
- #endif
- #if defined(__INTEL__)
- #pragma pack(push,2)
- #endif
- #endif
-
- /************************************************************************/
- /* */
- /* Heap Management Stuff */
- /* */
- /************************************************************************/
-
- /* autoptr
- *
- * Automatic pointer, like auto_ptr, except that I did it myself.
- *
- * The rules are simple: this is a substitute for T*ptr, except that
- * I cannot manipulate the pointer, I cannot copy to another pointer,
- * and I must either explicitly release the pointer to the world
- * (like TWindow objects) or return the value--both cause this pointer
- * to become NULL.
- */
-
- template <class T> class autoptr {
- public:
- autoptr(T *ptr = NULL) { v = ptr; }
- ~autoptr() { if (v) delete v; }
- const autoptr& operator = (T* ptr) { v = ptr; return *this; }
-
- /*
- * Access to pointer
- */
-
- /*
- * If the compiler barfs on the next line, it's because
- * you declared an autoptr<> with a fundamental type, such
- * as an int, a double, or a float.
- */
-
- T* operator ->() { return v; } // ERROR ^^^
-
-
- T& operator *() { return *v; }
- // T& operator [](long i) { return *(v+i); }
- int operator !() { return (v == NULL); }
- int operator == (const T* a) { return (v == a); }
- int operator != (const T* a) { return (v != a); }
- int operator == (const autoptr& ptr) { return (v == ptr.v); }
- int operator != (const autoptr& ptr) { return (v != ptr.v); }
-
- /*
- * Random things I can do to this pointer
- */
-
- void Detach() { v = NULL; }
- T* Return() { T *a; a = v; v = NULL; return a; }
- T* Ptr() { return v; }
-
- private:
- const autoptr& operator = (const autoptr& ptr) { return ptr; } // prohibit
-
- T *v;
- };
-
- #if defined(__MWERKS__)
- #if defined(macintosh)
- #pragma options align=reset
- #endif
- #if defined(__INTEL__)
- #pragma pack(pop)
- #endif
- #endif
-
- #endif // __XAUTOPTR_H__
-